package com.baibei.pay.selector;


import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baibei.model.SelecterInfo;
import com.baibei.pay.R;
import com.baibei.pay.R2;
import com.blankj.utilcode.utils.ScreenUtils;
import com.rae.widget.ScrollerNumberPicker;
import com.rae.widget.dialog.impl.SlideDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by shenminjie on 2017/8/8.
 * mail:shenminjie@100bei.com
 */

public class InfoSelecterFragment<T extends SelecterInfo> extends DialogFragment {

    @BindView(R2.id.picker)
    ScrollerNumberPicker mPicker;
    @BindView(R2.id.container)
    LinearLayout mContainer;

    Unbinder unbinder;

    /**
     * 数据源
     */
    private List<T> mInfos;

    /**
     * callback
     */
    private OnSelectListener<T> mListener;

    /**
     * 设置数据
     *
     * @param infos infos
     * @return return
     */
    public InfoSelecterFragment<T> setInfos(List<T> infos) {
        mInfos = infos;
        return this;
    }

    /**
     * 设置cb
     *
     * @param listener listener
     */
    public InfoSelecterFragment<T> setListener(OnSelectListener<T> listener) {
        mListener = listener;
        return this;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_infoselecter, container, false);
        unbinder = ButterKnife.bind(this, view);
        ViewGroup.LayoutParams layoutParams = mContainer.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth();
        mContainer.setLayoutParams(layoutParams);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updatePickerInfos();
    }

    /**
     * 更新数据
     */
    private void updatePickerInfos() {
        ArrayList<String> items = new ArrayList<>();
        for (T info : mInfos) {
            items.add(info.getItemName());
        }
        mPicker.setData(items);
    }

    @OnClick(R2.id.tv_cancle)
    public void onCancle() {
        if (mListener != null) {
            mListener.onCancle(this);
        }
    }

    @OnClick(R2.id.tv_define)
    public void onDefine() {
        if (mListener != null) {
            mListener.onDefineListener(mInfos.get(mPicker.getSelected()), this);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new SlideDialog(getContext());
    }

    @Override
    public void onDestroyView() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        super.onDestroyView();
    }


}
